<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script>






$(function(){
	//注册一个queryContentBtn搜索按钮事件
	$("#queryContentBtn").click(function(){
		//发送给servlet的数据(经过序列化)
		var uploadData =$("#queryContent").serialize();
		$.ajax({
			url:"QueryContentServlet",
			type:'GET',
			data:uploadData,
			dataType:'json',
			success:function(data){
				
				$(".mydata").remove();//把数据区域清空
				
				var html = "";
				if (data.length==0){
					$("#datanull").show();
				}
				
				$.each(data,function(i,notice){
					html = html + "<tr class='mydata'>";
					
					
					html = html + "<td>";
					html = html + "<input type='checkbox' name='cb' class='cb'";
					
					html = html + " value=" + notice.id;
					html = html + ">";
					html = html + "</td>";
					
					html = html + "<td>";
					html = html + (i+1);
					html = html + "</td>";
					
					html = html + "<td>";
					html = html + notice.title;
					html = html + "</td>";
					
					html = html + "<td>";
					html = html + notice.author;
					html = html + "</td>";
					
					html = html + "<td>";
					html = html + notice.content;
					html = html + "</td>";
					
					html = html + "<td>";
					if (notice.notice_status==1){
						html = html + "<font color='red'>未审核</font>";
					}else{
						html = html + "已审核";
						
					}
					
					html = html + "</td>";
					
					html = html + "<td>";
					html = html + notice.public_date;
					html = html + "</td>";
					
					html = html + "<td>";
					html = html + "<a href='DeteleNoticeServlet?id=";
					html = html + notice.id;
					html = html + "'>删除</a>&nbsp;";
					html = html + "<a href='UpdateNoticeViewServlet?id=";
					html = html + notice.id;
					html = html + "'>编辑</a>";
					html = html + "</td>";
					

					
					html = html + "</tr>";
				});
				$("#mytable").append(html);
			},
			timeout:3000,
			error:function(){
				alert('查询异常');
			}	
			
		});
	});
	
	
	//注册一个delBtn删除按钮事件
	$("#delBtn").click(function(){
		//包装集的序列号
		//格式：cb=331b57547cc511e78bf71c1b0daf345d&cb=331b96d67cc511e78bf71c1b0daf345d
		//正好适合Servlet接收
		var tt = $(".cb").serialize();
		$.ajax({   //ajax  特点 ：局部刷新 异步  用户可以操作其他内容
			url:"DeteleNoticesServlet",
			type:'GET',
			data:tt,
			dataType:'text',
			success:function(data){
				if (data=='false'){
					alert('服务端返回结果说你没选中');
				}else{
					alert('成功删除');
					location.href='index.html';
				}
			},
			timeout:3000,
			error:function(){
				
			}
		});
	});
	
	
	//注册点击全选checkbox事件
	$("#cbBtn").click(function(){
		//判断自己的状态
		var flag =   $(this).is(':checked');
		
		if (flag){
			//alert('全选');
			
			$(".cb").attr("checked",true);
		}
		else{
			//alert('取消选中');
			$(".cb").attr("checked",false);
		}
		//1.自己是选中(a.自己不选 b.把下面checkbox也取消选中)
		
		//2.自己是未选中状态(a.自己选中 b.把下面checkbox也选中)
	});
	
	//注册点击添加按钮事件
	$("#addBtn").click(function(){
		location.href='add.jsp';
	});
	
	
	var MySuccess = function(data){
		var html = "";
		if (data.length==0){
			$("#datanull").show();
		}
		
		$.each(data,function(i,notice){
			html = html + "<tr class='mydata'>";
			
			
			html = html + "<td>";
			html = html + "<input type='checkbox' name='cb' class='cb'";
			
			html = html + " value=" + notice.id;
			html = html + ">";
			html = html + "</td>";
			
			html = html + "<td>";
			html = html + (i+1);
			html = html + "</td>";
			
			html = html + "<td>";
			html = html + notice.title;
			html = html + "</td>";
			
			html = html + "<td>";
			html = html + notice.author;
			html = html + "</td>";
			
			html = html + "<td>";
			html = html + notice.content;
			html = html + "</td>";
			
			html = html + "<td>";
			if (notice.notice_status==1){
				html = html + "<font color='red'>未审核</font>";
			}else{
				html = html + "已审核";
				
			}
			
			html = html + "</td>";
			
			html = html + "<td>";
			html = html + notice.public_date;
			html = html + "</td>";
			
			html = html + "<td>";
			html = html + "<a href='DeteleNoticeServlet?id=";
			html = html + notice.id;
			html = html + "'>删除</a>&nbsp;";
			html = html + "<a href='UpdateNoticeViewServlet?id=";
			html = html + notice.id;
			html = html + "'>编辑</a>";
			html = html + "</td>";
			

			
			html = html + "</tr>";
		});
		$("#mytable").append(html);
	};
	
	var MyError = function(){
		alert('AJAX访问后台时出错');
	}
	
	$.ajax({
		url:"ListAllNoticeServlet",
		type:'GET',
		//data:
		dataType:'json',
		success:MySuccess,
		timeout:3000,
		error:MyError
	});
	
	
});

</script>
</head>
<body>
<div>通告信息列表</div>
<div><button id="addBtn">发表通告</button></div>

<div>
	内容：<input type="text" name="queryContent" id="queryContent">
		<button name="queryContentBtn" id="queryContentBtn" >搜索</button>
</div>


<table border="1" id="mytable">
	<tr>
		<td><input type="checkbox" id="cbBtn"></td>
		<td>编号</td>
		<td>标题</td>
		<td>发布者</td>
		<td>内容</td>
		<td>状态</td>
		<td>发布时间</td>
		<td>操作</td>
	</tr>
	<tr style="display: none;" id="datanull">
		<td colspan='7'><font color='red'>数据为空</font></td>
	</tr>
</table>
<button id="delBtn">删除</button>
</body>
</html>